# Causal Mediation Analysis for Treatment 4 (Online, Substitute) for Support and Trust:

# Effect of the IV (Treatment 4, Online, Substitute) onto the Mediators: 
affect_mediator <- lm(affect~treatment4,data=data)
calulation_mediator <- lm(calculation~treatment4,data=data)
substitution_mediator <- lm(substitution~treatment4,data=data)
optimism_mediator <- lm(optimism~treatment4,data=data)
transparency_mediator <- lm(transparency~treatment4,data=data) 

affect1 <- broom::tidy(affect_mediator, conf.int = TRUE)
calulation1 <- broom::tidy(calculation_mediator, conf.int = TRUE)
substitution1 <- broom::tidy(substitution_mediator, conf.int = TRUE)
optimism1 <- broom::tidy(optimsim_mediator, conf.int = TRUE)
transparency1 <- broom::tidy(transparency_mediator, conf.int = TRUE)

bounds1a <- cbind(affect1$estimate, affect1$conf.low, affect1$conf.high) 
bounds1a <- cbind(bounds1a[2,1], bounds1a[2,2], bounds1a[2,3])
bounds2a <- cbind(calculation1$estimate, calculation1$conf.low, calculation1$conf.high) 
bounds2a <- cbind(bounds2a[2,1], bounds2a[2,2], bounds2a[2,3])
bounds3a <- cbind(substitution1$estimate, substitution1$conf.low, substitution1$conf.high) 
bounds3a <- cbind(bounds3a[2,1], bounds3a[2,2], bounds3a[2,3])
bounds4a <- cbind(optimism1$estimate, optimism1$conf.low, optimsim1$conf.high) 
bounds4a <- cbind(bounds4a[2,1], bounds4a[2,2], bounds4a[2,3])
bounds5a <- cbind(transpaency1$estimate, transparency1$conf.low, transparency1$conf.high) 
bounds5a <- cbind(bounds5a[2,1], bounds5a[2,2], bounds5a[2,3])

ci_total1 <- rbind(bounds1a,bounds2a,bounds3a,bounds4a,bounds5a)
ci_labels1 <- rbind(1,2,3,4,5)
ci_results1 <- data.frame(cbind(ci_labels1,ci_total1))
ci_results1 <- round(ci_results1, digits = 3)
colnames(ci_results1) = c("Mechanism", "PE", "Lower", "Upper")

ggplot(ci_results1,aes(x=(Mechanism), y=(PE), ymin=Lower,ymax=Upper)) +
  geom_point() +
  geom_errorbar(width=0) +
  geom_text(aes(label = PE), vjust = -0.5)  + 
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  theme_bw() +
  scale_x_discrete(limits=c("FOMO","Calculation","Efficiency","Optimism","Transparency")) +
  scale_y_continuous(limits=c(-1, 1)) +
  xlab("Mechanisms") +
  ylab("Effect of Social Media Content Moderation to Substitute for Human Judgment on Mediators")

#b) Effect of the Mediators on the Dependent Variable: 
affect_dv1 <- lm(support[trust]~treatment4+affect,data=data)
calculation_dv1 <- lm(support[trust]~treatment4+calculation,data=data)
substitution_dv1 <- lm(support[trust]~treatment4+substitution,data=data)
optimsim_dv1 <- lm(support[trust]~treatment4+optimism,data=data)
transparency_dv1 <- lm(support[trust]~treatment4+transparency,data=data)

affect2  <- broom::tidy(affect_dv1, conf.int = TRUE)
calculation2 <- broom::tidy(calculation_dv1, conf.int = TRUE)
substitution2 <- broom::tidy(substitution_dv1, conf.int = TRUE)
optimism2 <- broom::tidy(optismsm_dv1, conf.int = TRUE)
transparency2 <- broom::tidy(transparency_dv1, conf.int = TRUE)

bounds1b <- cbind(affect2$estimate, affect2$conf.low, affect2$conf.high) 
bounds1b <- cbind(bounds1b[3,1], bounds1b[3,2], bounds1b[3,3])
bounds2b <- cbind(calculation2$estimate, ccalculation2$conf.low, calculation2$conf.high) 
bounds2b <- cbind(bounds2b[3,1], bounds2b[3,2], bounds2b[3,3])
bounds3b <- cbind(substitution2 $estimate, substitution2 $conf.low, substitution2 $conf.high) 
bounds3b <- cbind(bounds3b[3,1], bounds3b[3,2], bounds3b[3,3])
bounds4b <- cbind(optimism2$estimate, optimism2$conf.low, optimism2$conf.high) 
bounds4b <- cbind(bounds4b[3,1], bounds4b[3,2], bounds4b[3,3])
bounds5b <- cbind(transparency2$estimate, transparency2$conf.low, transparency2$conf.high) 
bounds5b <- cbind(bounds5b[3,1], bounds5b[3,2], bounds5b[3,3])

ci_total2 <- rbind(bounds1b,bounds2b,bounds3b,bounds4b,bounds5b)
ci_labels2 <- rbind(1,2,3,4,5)
ci_results2 <- data.frame(cbind(ci_labels2,ci_total2))
ci_results2 <- round(ci_results2, digits = 3)
colnames(ci_results2) = c("Mechanism", "PE", "Lower", "Upper")

ggplot(ci_results2,aes(x=(Mechanism), y=(PE), ymin=Lower,ymax=Upper)) +
  geom_point() +
  geom_errorbar(width=0) +
  geom_text(aes(label = PE), vjust = -0.5)  + 
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  theme_bw() +
  scale_x_discrete(limits=c("FOMO","Calculation","Efficiency","Optimism","Transparency")) +
  scale_y_continuous(limits=c(-1, 1)) +
  xlab("Mechanisms") +
  ylab("Effect of Mediators on Support[Trust]")

# Estimated Causal Pathways:
affect_results1 <- mediate(affect_mediator,affect_dv1,treat='os',mediator='affect') 
calculation_results1 <- mediate(calculation_mediator,calculation_dv1,treat='os',mediator='calculation')
substitution_results1 <- mediate(substitution_mediator,substitution_dv1,treat='os',mediator='substitution')
optimsim_results1 <- mediate(optimism_mediator,optimism_dv1,treat='os',mediator='optimism')
transparency_results1 <- mediate(transparency_mediator,transparency_dv1,treat='os',mediator='transparency') 

affect3  <- broom::tidy(affect_results1, conf.int = TRUE)
calculation3 <- broom::tidy(calculation_results1, conf.int = TRUE)
sustitution3 <- broom::tidy(substitution_results1, conf.int = TRUE)
optimsim3 <- broom::tidy(optimsim_results1, conf.int = TRUE)
transparency3 <- broom::tidy(transparency_results1, conf.int = TRUE)

bounds1c <- cbind(affect3$estimate, affect3$conf.low, affect3$conf.high) 
bounds1c <- cbind(bounds1c[1,1], bounds1c[1,2], bounds1c[1,3])
bounds2c <- cbind(calculation3$estimate, calculation3$conf.low, calculation3$conf.high) 
bounds2c <- cbind(bounds2c[1,1], bounds2c[1,2], bounds2c[1,3])
bounds3c <- cbind(sustitution3$estimate, sustitution3$conf.low, sustitution3$conf.high) 
bounds3c <- cbind(bounds3c[1,1], bounds3c[1,2], bounds3c[1,3])
bounds4c <- cbind(optimsim3$estimate, optimsim3$conf.low, optimsim3$conf.high) 
bounds4c <- cbind(bounds4c[1,1], bounds4c[1,2], bounds4c[1,3])
bounds5c <- cbind(transparency3$estimate, transparency3$conf.low, transparency3$conf.high) 
bounds5c <- cbind(bounds5c[1,1], bounds5c[1,2], bounds5c[1,3])

ci_total3 <- rbind(bounds1c,bounds2c,bounds3c,bounds4c,bounds5c)
ci_labels3 <- rbind(1,2,3,4,5)
ci_results3 <- data.frame(cbind(ci_labels3,ci_total3))
ci_results3 <- round(ci_results3, digits = 3)
colnames(ci_results3) = c("Mechanism", "PE", "Lower", "Upper")

ggplot(ci_results3,aes(x=(Mechanism), y=(PE), ymin=Lower,ymax=Upper)) +
  geom_point() +
  geom_errorbar(width=0) +
  geom_text(aes(label = PE), vjust = -0.5)  + 
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  theme_bw() + 
  scale_x_discrete(limits=c("FOMO","Calculation","Efficiency","Optimism","Transparency"))  +
  xlab("Mechanisms") +
  scale_y_continuous(limits=c(-1, 1)) +
  ylab("Indirect Effect Attributed to Mechanisms")

# Sobel-Goodman Robustness Check:
mediation.test(data$affect,data_cm$treatment4,data$support[trust])
mediation.test(data$calculation,data_cm$treatment4,data$support[trust])
mediation.test(data$substitution,data_cm$treatment4,data$support[trust])
mediation.test(data$optimism,data_cm$treatment4,data$support[trust])
mediation.test(data$transparency,data_cm$treatment4,data$support[trust])